<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" v-enterToNext>
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('商户编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantCode">
              <a-input v-model="model.merchantCode" :placeholder="$t('请输入商户编码')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('门店类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="location">
              <j-dict-select-tag type="list" v-model="model.location" @input="locationChange" dictCode="bis_shop.location" :placeholder="$t('请选择')" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('门店编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" @change="codeChange" :placeholder="$t('请输入门店编码')" :disabled="disable || model.location == 0"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('卡店码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardCode">
              <a-input v-model="model.cardCode" :placeholder="$t('请输入卡店码')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('门店名称')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" :placeholder="$t('请输入门店名称')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('系统类型')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-select v-model="model.type" :placeholder="$t('请选择')">
                <a-select-option v-for="item in typeList" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('原始主机号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hostId">
              <a-input
                v-model="model.hostId"
                :placeholder="$t('格式为商户编码+主机号，例：860311901_i-0jl2iuwjqvw9ut7q3n5z')"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('状态')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="state">
              <j-switch v-model="model.state"></j-switch>
            </a-form-model-item>
          </a-col>
          <a-col  :span="24">
            <a-form-model-item :label="$t('有效期')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <j-date
                :show-time="false"
                valueFormat="YYYY-MM-DD"
                :placeholder="$t('请选择开始日期')"
                class="query-group-cust"
                v-model="model.beginDate"
              ></j-date>~
              <j-date
                :show-time="false"
                valueFormat="YYYY-MM-DD"
                :placeholder="$t('请选择结束日期')"
                class="query-group-cust"
                v-model="model.endDate"
                @change="validatorEndDate"
              ></j-date>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('负责人')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contact">
              <a-input v-model="model.contact" :placeholder="$t('请输入负责人')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('负责人电话')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="phone">
              <a-input v-model="model.phone" :placeholder="$t('请输入负责人电话')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('负责人邮箱')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="email">
              <a-input v-model="model.email" :placeholder="$t('请输入负责人邮箱')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('客服姓名')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serverName">
              <a-input v-model="model.customerServiceName" :placeholder="$t('请输入客服姓名')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('客服电话')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serverPhone">
              <a-input v-model="model.customerServicePhone" :placeholder="$t('请输入客服电话')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('官网地址')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="officialWebsite">
              <a-input v-model="model.officialSite" :placeholder="$t('请输入官网地址')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('门店地址')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="address">
              <a-input v-model="model.address" :placeholder="$t('请输入门店地址')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('备注')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comment">
              <a-input v-model="model.comment" :placeholder="$t('请输入备注')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('图片')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopPicture">
              <j-image-upload isMultiple :number="1" v-model="model.shopPicture"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item>
              <j-map @getMapValue="getMapValue" :city="defaultValue" :centerPoint="centerPoint"></j-map>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import JMap from '../../goods/modules/JMap'
import moment from 'moment'

export default {
  name: 'BisShopForm',
  components: {
    JMap,
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    var validatorDate = (rule, value, callback) => {
      let that = this;
      if(!that.model.beginDate || !that.model.endDate){
        console.log(callback)
        return callback(new Error(this.$t('请选择有效期')));
      }else{
        callback();
      }
    };
    return {
      moment,
      model: {
        merchantCode: window.location.host.split('.')[0]||'dev',
        location: '1',
        state: 'Y',
        beginDate: moment().format('YYYY-MM-DD'),
        endDate: moment().add(1,"years").format('YYYY-MM-DD'),
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        location: [
          { required: true, message: this.$t('请选择门店类型') },
        ],
        code: [
          { required: true, message: this.$t('请输入门店编码') },
          {
            validator: (rule, value, callback) =>
              validateDuplicateValue('bis_shop', 'code', value, this.model.id, callback),
          },
        ],
        cardCode: [
          { required: true, message: this.$t('请输入卡店码') },
        ],
        name: [
          { required: true, message: this.$t('请输入门店名称') },
          {
            validator: (rule, value, callback) =>
              validateDuplicateValue('bis_shop', 'name', value, this.model.id, callback),
          },
        ],
        type: [{ required: true, message: this.$t('请选择系统类型') }],
        hostId: [{ required: false, message: this.$t('请输入原始主机号') }],
        state: [{ required: true, message: this.$t('请输入状态') }],
        beginDate: [
          { required: true, message: this.$t('请选择有效期') },
          {
            validator: validatorDate,
          },
        ],
        contact: [{ required: false, message: this.$t('请输入负责人') }],
        phone: [{ required: false, message: this.$t('请输入负责人电话') }],
        email: [{ required: false, message: this.$t('请输入负责人邮箱') }],
        customerServiceName: [{ required: false, message: this.$t('请输入客服姓名') }],
        customerServicePhone: [{ required: false, message: this.$t('请输入客服电话') }],
        officialSite: [{ required: false, message: this.$t('请输入官网地址') }],
        address: [{ required: false, message: this.$t('请输入门店地址') }],
      },
      url: {
        add: '/shop/bisShop/add',
        edit: '/shop/bisShop/edit',
        queryById: '/shop/bisShop/queryById',
      },
      disable: false,
      disable1: false,
      defaultValue: [],
      centerPoint: '',
      typeList:[],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    locationChange(){
      if(this.model.location == 0){
        this.$set(this.model,"code", "0");
        this.$set(this.model,"cardCode", "0");
      } else {
        this.$set(this.model,"code", "");
        this.$set(this.model,"cardCode", "");
      }
    },
    codeChange(e){
      this.$set(this.model,"cardCode",e.target.value);
    },
    validatorEndDate(){
      this.$refs.form.validate('beginDate');
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.model.id) {
        this.disable = true
        if(this.model.longitude){
          this.$set(this,'centerPoint', this.model.longitude+','+this.model.latitude)
          this.$set(this,'defaultValue', [this.model.longitude,this.model.latitude])
        }
      }
      if (this.model.hostId) {
        this.disable1 = true
      }
      if(!this.model.merchantCode){
        this.model.merchantCode = window.location.host.split('.')[0]||'dev'
      }
      getAction('/system/bisSysType/list',{pageNo: 1,pageSize: 999}).then(res=>{
        if(res.success){
          this.typeList = res.result.records
        }else{
          this.$message.warning(res.message)
        }
      })
    },
    getMapValue(obj) {
      this.$set(this.model, 'longitude', obj.lng)
      this.$set(this.model, 'latitude', obj.lat)
      this.$set(this.model, 'address', obj.address)
    },
    submitForm() {
      let that = this
      // 触发表单验证
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!that.model.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          httpAction(httpurl, that.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>